package jp.test;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;

import org.cybergarage.http.HTTPRequest;
import org.cybergarage.http.HTTPServerList;
import org.cybergarage.upnp.ControlPoint;
import org.cybergarage.upnp.DeviceList;
import org.cybergarage.upnp.UPnP;
import org.cybergarage.http.HTTPRequestListener;





public class Gateway  {
	public static int DEFAULT_WIDTH = 640;
	public final static int DEFAULT_HEIGHT = 480;
	
	private JFrame frame;
	private MenuBar menuBar;
	private GatewayControlPanelPane gatewayControlPanel;
	private SAServer sa;
	private DAServer da;
	
	
	public Gateway()
	{
		
		
		startSA();
		startDA();
		
		
		initFrame();
		startGC();
	}
	
	private void initFrame()
	{
		frame = new JFrame("Gateway ControlPanel");
		
		frame.getContentPane().setLayout(new BorderLayout());
		
		menuBar = new MenuBar();
		frame.setJMenuBar(menuBar);
		
		gatewayControlPanel = new GatewayControlPanelPane();
		gatewayControlPanel.setGateway(this);
		frame.getContentPane().add(gatewayControlPanel, BorderLayout.CENTER);
		
		frame.setSize(new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}
	
	private void startDA(){
		////////////////////////////////////////
		// DA JOIN Server
		////////////////////////////////////////
		
		int retryCnt = 0;
		int bindPort = 7000;
		HTTPServerList httpServerList = new HTTPServerList();
		while (httpServerList.open(bindPort) == false) {
			retryCnt++;
			if (UPnP.SERVER_RETRY_COUNT < retryCnt){
				System.err.println("Fail to start DA server");
				return;
			}
			bindPort++;
		}
		this.da = new DAServer();
		httpServerList.addRequestListener(da);
		httpServerList.start();
		System.out.println("DA run on "+bindPort);
	}
	
	private void startSA(){
		////////////////////////////////////////
		// SA UPDATE server
		////////////////////////////////////////
		
		int retryCnt = 0;
		int bindPort = 7100;
		HTTPServerList httpServerList = new HTTPServerList();
		while (httpServerList.open(bindPort) == false) {
			retryCnt++;
			if (UPnP.SERVER_RETRY_COUNT < retryCnt){
				System.err.println("Fail to start DA server");
				return;
			}
			bindPort++;
		}
		this.sa = new SAServer();
		httpServerList.addRequestListener(sa);
		httpServerList.start();
		System.out.println("SA started port:"+bindPort);
	}
	
	public void startGC(){
		String file1 = "description/description_adevice.xml";
		try{
			DDevice d1 = new DDevice(file1);
			d1.start();
		}catch(Exception e){}
		
	}
	
	public static void main(String args[]){
		new Gateway();
		
		/*
		 * SA
		 * 1 start group controller ---> got a group key LOCAL
		 * 
		 * 2 collect device list
		 * 
		 * 3 request to DA authen and update group key REMOTE
		 * 
		 * 4 rekey
		 * 
		 * 5 Revoke service
		 * 
		 * END
		 */
		
		
		
		/*
		 * DA
		 * 1 start group controller --> got a group key LOCAL
		 * 
		 * 2 collect device list
		 * 
		 * 3 accept request from SA authen and update group key REMOTE
		 * 
		 * 4 rekey
		 * 
		 * 5 UPDATE
		 */
		
		
		

	}
	

	
	private DeviceList localDeviceList;
	private DeviceList remoteDeviceList;
	
	private DDevice groupControllerDevice;
	private ControlPoint controlPoint;
	
	public void collectLocalDeviceList(){
		
	}
	
	public void updateSharedDevice(){
		
	}
	
	public void updateRemoteDeviceList(){
		
	}
	
	public void connectDA(String name){
		sa.connectDA(name);
	}
}
